﻿using AspNetExpressInformationManageSystem.Models;
using Microsoft.AspNet.Identity;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Net;
using System.Web.Mvc;

namespace AspNetExpressInformationManageSystem.Controllers
{
    [Authorize(Roles = "Distributor")]
    public class ExpressGoodsStoragesController : Controller
    {
        private ExpressDbContext db = new ExpressDbContext();

        // GET: ExpressGoodsStorages
        public ActionResult Index()
        {
            var expressGoodsStorage = db.ExpressGoodsStorage.Include(e => e.ExpressGoods).Include(e => e.ExpressStorage);
            return View(expressGoodsStorage.ToList());
        }

        public ActionResult Select()
        {
            return View(db.ExpressGoods.ToList());
        }

        // GET: ExpressGoodsStorages/Details/5
        public ActionResult Details(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            ExpressGoodsStorage expressGoodsStorage = db.ExpressGoodsStorage.Find(id);
            if (expressGoodsStorage == null)
            {
                return HttpNotFound();
            }
            expressGoodsStorage.ExpressGoods = db.ExpressGoods.Find(expressGoodsStorage.ExpressGoodsID);
            expressGoodsStorage.ExpressStorage = db.ExpressStorage.Find(expressGoodsStorage.ExpressStorageID);
            return View(expressGoodsStorage);
        }

        // GET: ExpressGoodsStorages/Create
        public ActionResult Create(String GoodsStorageID)
        {
            ViewBag.MultiExpressGoodsID = GoodsStorageID;
            ViewBag.ExpressGoodsID = new SelectList(db.ExpressGoods, "ID", "ID");
            ViewBag.ExpressStorageID = new SelectList(db.ExpressStorage, "ID", "Name");
            return View(new MultiExpressGoodsStorage() { ExpressGoodsID = GoodsStorageID, Action = "入库", Operator = User.Identity.GetUserName(), Date = DateTime.Now, Status = "在库" });
        }

        // POST: ExpressGoodsStorages/Create
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see https://go.microsoft.com/fwlink/?LinkId=317598.

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create([Bind(Include = "ID,ExpressGoodsID,ExpressStorageID,Action,Operator,Date,Status")] MultiExpressGoodsStorage multiExpressGoodsStorage)
        {
            if (ModelState.IsValid)
            {
                var goodsIds = multiExpressGoodsStorage.ExpressGoodsID.Split(',');
                foreach (var id in goodsIds)
                {;
                    ExpressGoodsStorage expressGoodsStorage = new ExpressGoodsStorage(multiExpressGoodsStorage, Convert.ToInt32(id));
                    db.ExpressGoodsStorage.Add(expressGoodsStorage);
                    var expressGoods = db.ExpressGoods.Find(expressGoodsStorage.ExpressGoodsID);
                    var expressStorage = db.ExpressStorage.Find(expressGoodsStorage.ExpressStorageID);
                    expressGoods.Status = "运往" + expressStorage.Name;
                    db.Entry(expressGoods).State = EntityState.Modified;
                    db.SaveChanges();
                }
                return RedirectToAction("Index");
            }
            return View(multiExpressGoodsStorage);
        }

        // GET: ExpressGoodsStorages/Edit/5
        public ActionResult Edit(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            ExpressGoodsStorage expressGoodsStorage = db.ExpressGoodsStorage.Find(id);
            if (expressGoodsStorage == null)
            {
                return HttpNotFound();
            }
            ViewBag.ExpressGoodsID = new SelectList(db.ExpressGoods, "ID", "ID", expressGoodsStorage.ExpressGoodsID);
            ViewBag.ExpressStorageID = new SelectList(db.ExpressStorage, "ID", "Name", expressGoodsStorage.ExpressStorageID);
            return View(expressGoodsStorage);
        }

        // POST: ExpressGoodsStorages/Edit/5
        // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
        // more details see https://go.microsoft.com/fwlink/?LinkId=317598.
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit([Bind(Include = "ID,ExpressGoodsID,ExpressStorageID,Action,Operator,Date,Status")] ExpressGoodsStorage expressGoodsStorage)
        {
            if (ModelState.IsValid)
            {
                db.Entry(expressGoodsStorage).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.ExpressGoodsID = new SelectList(db.ExpressGoods, "ID", "ID", expressGoodsStorage.ExpressGoodsID);
            ViewBag.ExpressStorageID = new SelectList(db.ExpressStorage, "ID", "Name", expressGoodsStorage.ExpressStorageID);
            return View(expressGoodsStorage);
        }

        // GET: ExpressGoodsStorages/Delete/5
        public ActionResult Delete(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            ExpressGoodsStorage expressGoodsStorage = db.ExpressGoodsStorage.Find(id);
            if (expressGoodsStorage == null)
            {
                return HttpNotFound();
            }
            return View(expressGoodsStorage);
        }

        // POST: ExpressGoodsStorages/Delete/5
        [HttpPost, ActionName("Delete")]
        [ValidateAntiForgeryToken]
        public ActionResult DeleteConfirmed(int id)
        {
            ExpressGoodsStorage expressGoodsStorage = db.ExpressGoodsStorage.Find(id);
            db.ExpressGoodsStorage.Remove(expressGoodsStorage);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }
    }
}
